package cn.edu.hcnu.controller;

import cn.edu.hcnu.domain.User;
import cn.edu.hcnu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @description:
 * @author: Administrator
 * @time: 2023/6/19 18:17
 */
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/list")
    List<User> list() {
        return userService.getAll();
    }

    @GetMapping("/{id}")
    List<User> get(@PathVariable("id") Integer id) {
        return Collections.singletonList(userService.getUserById(id));
    }

    @PostMapping("/add")
    User insert(@RequestBody User user) {
        return userService.addUser(user);
    }

    @PostMapping("/update")
    User update(@RequestBody User user) {
        return userService.updateUser(user);
    }

    @DeleteMapping("/{id}")
    int delete(@PathVariable("id") Integer id) {
        return userService.deleteUser(id);
    }
}
